package com.bailiban.aigou.admin.dao.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import com.bailiban.aigou.admin.dao.SearchHistoryDao;

import com.bailiban.aigou.admin.entity.SearchHistory;
@Repository
public class SearchHistoryDaoImpl implements SearchHistoryDao {
	
	@Resource
	private SessionFactory sessionFactory;
	/**
	 * 获取指定条数的热搜词
	 * @param num
	 * @return
	 * @
	 */
	@Override
	public List<SearchHistory> getSearchHistoryPages(int num) {
		Session session = sessionFactory.getCurrentSession();
		List<SearchHistory> list = new ArrayList<SearchHistory>();
		Query query = session.createQuery("from SearchHistory order by num desc ");
		query.setMaxResults(num);
		list=query.list();
		
	
	   
		return list;
	}
	/**
	 * 新增热搜词
	 * @param searchHistory
	 * @
	 */
	@Override
	public void addSearchHistory(SearchHistory searchHistory) {
		sessionFactory.getCurrentSession().save(searchHistory);

	}
	/**
	 * 热搜词是否存在
	 * @param search
	 * @return
	 * @
	 */
	@Override
	public boolean isExist(String searchWords) {
		Session session = sessionFactory.getCurrentSession();
		SearchHistory searchHistory=session.get(SearchHistory.class, searchWords);
		boolean ischeck=false;
		if(searchHistory!=null){
			ischeck=true;
		}
		return ischeck;
	}
	/**
	 * 更新热搜词次数（每次+1）
	 * @param search
	 * @
	 */
	@Override
	public void updateSearchHistory(String searchWords) {
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("UPDATE SearchHistory SET num = num + 1 WHERE searchWords = ?");
		query.setString(0, searchWords);
	}

}
